﻿var approotpath = "";

var containerTypes = { 'options': 1, 'html': 2, 'li': 3 };
function CatDetails() {
    this.nItemId = 0;
    this.sItemName = "";
    this.sItemSlug = "";
    this.sItemDescription = "";
    this.sItemImagePath = "";
    this.nItemStatus = 0;
}

function SubCatDetails() {
    this.nItemId = 0;
    this.nParentId = 0;
    this.sParentName = 0;
    this.sItemName = "";
    this.sItemSlug = "";
    this.sItemDescription = "";
    this.sItemImagePath = "";
    this.nItemStatus = 0;
}

function ProductDetails() {
    this.nPid = 0;
    this.nCid = 0;
    this.sCatName = "";
    this.nSCid = 0;
    this.sSubCatName = "";
    this.sTitle = "";
    this.sSlug = "";
    this.sDesc = "";
    this.sHomelnk = "";
    this.sDownloadlnk = "";
    this.sBuyNowlnk = "";
    this.sCompanyName = "";
    this.nImgCounts = 0;
    this.dBasePrice = 0.0;
    this.nCurrency = 0;
    this.sCurrency = "";
    this.nDiscountOffered = 0;
    this.dNetPrice = 0.0;
}

function getAppPath(spath) {
    if (isNaN(spath) && spath != "")
        return (rootpath + (spath.replace('~/', '')));
    else
        return "";
}

//Class:: MyContentLoader
/// CUSTOM JS LIBRARY
/// CREATED BY: SUJEET GUPTA
/// CREATED ON: 01-Jan-2015 
/// LICENCE: AS PER MIT LICENCE


//show/hide loader
var MyContentLoader=function(){
    this.LastAccessedContainerId = "";
    this.isLoadingTextGif = 1; //means (1) wait.GIF ==> Loading.... (2) 20X20gif.GIF ==> small gif
};
MyContentLoader.prototype.showLoader = function (containerId, cellpadding, _isTable) {
    if (document.getElementById(containerId)) {
        this.LastAccessedContainerId = containerId;

        var loaderImg = "<img src='" + approotpath + "Content/Images/wait.GIF' alt='loading..'/>";
        if (this.isLoadingTextGif==1) {
            loaderImg = "<img src='" + approotpath + "Content/Images/wait.GIF' alt='loading..'/>";
        }
        else if (this.isLoadingTextGif == 2) {
            loaderImg = "<img src='" + approotpath + "Content/Images/20X20gif.GIF' alt='loading..'/>";
        }

        if (document.getElementById(this.LastAccessedContainerId)) {
            
            $("#" + this.LastAccessedContainerId).find("#norecordsfound").remove();
            $("#" + this.LastAccessedContainerId + "_loader").remove();
        }

        if (_isTable) {
            var row = "<tr id='" + this.LastAccessedContainerId + "_loader'><td class='text-center' colspan='" + cellpadding + "' style='padding:20px;color:red'>" + loaderImg + "</td></tr>";
            $("#" + containerId).append(row);
        }
        else {
            $("#" + containerId).parent().append("<div id='" + this.LastAccessedContainerId + "_loader' style='float:left'>" + loaderImg + "</div>");
        }
        //approotpath
    }
};

MyContentLoader.prototype.hideLoader = function (containerId, cellpadding, _isTable) {
    if (typeof containerId !==undefined && this.LastAccessedContainerId != containerId)
        this.LastAccessedContainerId = containerId;

    if (document.getElementById(this.LastAccessedContainerId)) {
        $("#" + this.LastAccessedContainerId + "_loader").remove();
        this.LastAccessedContainerId = "";
        //approotpath
    }
};


MyContentLoader.prototype.noRecordExist = function (cellpadding, strMsg) {
    return "<tr id='norecordsfound'><td class='text-center' colspan='" + cellpadding + "' style='padding:20px;color:red'>" + strMsg + "</td></tr>";
};
//End-Class:: MyContentLoader

// HelperMethods - class
var HelperMethods=function(){
    this.invoke = true;
};
HelperMethods.prototype.contentManager = function (data, containerId, containerType) {
    var sContent = "";
    switch (containerType) {
        case containerTypes.options:
            if (data.errCode == 250) {
                sContent = "";
                var dataJson = $.parseJSON(data.msgJson);
                if (dataJson.length > 0) {
                    var key = "";
                    var value = "";
                    for (var items in dataJson) {
                        if (containerId.toLowerCase().indexOf("subcategory") >= 0 || containerId.toLowerCase().indexOf("category") >= 0) {
                            key = dataJson[items].nItemId;
                            value = dataJson[items].sItemName;
                        }
                        else if (containerId.toLowerCase().indexOf("currency") >= 0) {
                            key =dataJson[items].nID;
                            value = dataJson[items].sCurISO + " | " + dataJson[items].sCurName;
                        }
                        sContent += "<option value='" + key + "'>" + value + "</option>";
                    }
                }
            }
            if (sContent != "") {
                if (document.getElementById(containerId)) {
                    //clear all content and add new content
                    $("#" + containerId).find('option:gt(0)').remove();
                    $("#" + containerId).append(sContent);
                }
            }
            break;
        case containerTypes.html:
            break;
        case containerTypes.li:
            break;
    }
};

HelperMethods.prototype.getCategory = function (containerId, containerType) {
    var oMycontentLoader = new MyContentLoader();
    var oCatDetail = new CatDetails();
    oMycontentLoader.isLoadingTextGif = 2;//small gif 20x20
    oMycontentLoader.showLoader(containerId, 0, false);
    $.ajax(approotpath + "Helper/GetCategoryDetailsJSON", {
        data: {},
        type: "POST",
        dataType: 'json',
        success: function (data) {
            var oHelperMethods = new HelperMethods();
            oHelperMethods.contentManager(data, containerId, containerType);
        },
        error: function (jqXHR, status, err) {
            console.log("Error raised: " + err);
            oMycontentLoader.hideLoader(containerId, 0, false);
        },
        complete: function (jqXHR, status) {
            oMycontentLoader.hideLoader(containerId, 0, false);
        }
    });
};

HelperMethods.prototype.getSubCategory = function (parentId, containerId, containerType) {
    var oMycontentLoader = new MyContentLoader();
    var oSubCatDetail = new SubCatDetails();
    if (!isNaN(parentId) && parseInt(parentId)>0) {
        oSubCatDetail.nParentId = parentId;
    }
    oMycontentLoader.isLoadingTextGif = 2;//small gif 20x20
    oMycontentLoader.showLoader(containerId, 0, false);
    $.ajax(approotpath + "Helper/GetSubCategoryDetailsJSON", {
        data: oSubCatDetail,
        type: "POST",
        dataType: 'json',
        success: function (data) {
            var oHelperMethods = new HelperMethods();            
            oHelperMethods.contentManager(data, containerId, containerType);
        },
        error: function (jqXHR, status, err) {
            console.log("Error raised: " + err);
            oMycontentLoader.hideLoader(containerId, 0, false);
        },
        complete: function (jqXHR, status) {
            oMycontentLoader.hideLoader(containerId, 0, false);
        }
    });
};

HelperMethods.prototype.getCurrency = function (containerId, containerType) {
    var oMycontentLoader = new MyContentLoader();
    oMycontentLoader.isLoadingTextGif = 2;//small gif 20x20
    oMycontentLoader.showLoader(containerId, 0, false);
    
    $.ajax(approotpath + "Helper/GetCurrencyDetailsJSON", {
        data: {},
        type: "POST",
        dataType: 'json',
        success: function (data) {
            var oHelperMethods = new HelperMethods();
            oHelperMethods.contentManager(data, containerId, containerType);
        },
        error: function (jqXHR, status, err) {
            console.log("Error raised: " + err);
            oMycontentLoader.hideLoader(containerId, 0, false);
        },
        complete: function (jqXHR, status) {
            oMycontentLoader.hideLoader(containerId, 0, false);
        }
    });
};


// HelperMethods - End class

function getBasedCategory(containerId, containerType) {
    var oHelper = new HelperMethods();
    oHelper.getCategory(containerId, containerType);    
}

function getBasedSubCategory(parentId, containerId, containerType) {
    if (!isNaN(parentId) && parseInt(parentId) > 0) {
        var oHelper = new HelperMethods();
        oHelper.getSubCategory(parentId, containerId, containerType);
    }

}

function getBasedCurrency(containerId, containerType, curId) {
    var oHelper = new HelperMethods();
    if (!(parseInt(curId) > 0)) { // if cur id not available means need to load currency
        oHelper.getCurrency(containerId, containerType);
    }

}